<?php

namespace App\Http\Controllers;

use App\Models\ClassroomModels;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ClassRoomController extends Controller
{
    /**
     *  获取所有班级信息
     */
    public function index()
    {
        // 实例化模型
        $classModel = ClassroomModels::all();
        return response()->json([
            'msg' => '获取成功',
            'code' => 200,
            'data' => $classModel
        ]);
        // 返回所有数据
    }

    /**
     * 添加
     */
    public function create(Request $request)
    {
        // 添加班级
        $data = $request->input();
        // 实例化模型
        $classModel = new ClassroomModels();
        $classModel->name = $data['name'];
        $classModel->number = $data['number'];
        // 检查班级是否已经存在
        $existingClass = ClassroomModels::where('name', $classModel->name)->first();
        if (!empty($existingClass)) {
            return response()->json([
                'msg' => '班级已经存在',
                'code' => 0
            ]);
        }
        $date = $classModel->save();
        if ($date >= 1) {
            return response()->json([
                'msg' => '添加成功',
                'code' => 200,
            ]);
        } else {
            return response()->json([
                'msg' => '添加失败',
                'code' => 0,
            ]);
        }
    }

    /**
     *  获取单个数据
     */
    public function show(Request $request)
    {

        // 获取到数据
        $data = $request->input('id');
        // 使用模型进行查找
        $classModel = ClassroomModels::where('id', $data)->first();
        if (empty($classModel)){
            return response()->json([
                'msg' => '班级不存在',
                'code' => 0
            ]);
        }else{
            return response()->json([
                'msg' => '获取成功',
                'code' => 200,
                'data' => $classModel
            ]);
        }
    }

    /**
     *  修改班级接口
     */
    public function update(Request $request)
    {
        // 接收数据
        $data = $request->all();
        // 使用data里面得id查询班级是否存在 如果存在 允许修改
        $existingClass = ClassroomModels::where('id', $data['id'])->first();
        if (empty($existingClass)) {
            return response()->json([
                'msg' => '班级不存在',
                'code' => 0
            ]);
        }
        // 修改班级
        try {
            $existingClass->update([
                'name' => $data['name'],
                'number' => $data['number']
            ]);
            return response()->json([
                'msg' => '班级信息已更新',
                'code' => 1
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'msg' => '更新班级信息时出现错误' . $e->getMessage(),
                'code' => 0
            ]);
        }
    }

    /**
     * 删除班级
     */
    public function destroy(request $request)
    {
        // 接收数据
        $data = $request->input('id');
        // 检查班级是否已经存在
        $existingClass = ClassroomModels::where('id', $data)->first();
        if (empty($existingClass)) {
            return response()->json([
                'msg' => '没有这个班级',
                'code' => 0
            ]);
        }
        // 删除班级
        $existingClass = $existingClass->delete();
        if ($existingClass >= 1) {
            return response()->json([
                'msg' => '删除成功',
                'code' => 1
            ]);
        } else {
            return response()->json([
                'msg' => '删除失败',
                'code' => 0
            ]);
        }
    }
}
